import axios from 'axios'
import config from './config.js'
// import configer from '../../config/index'
// import Cookies from 'cookies-js'

// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'

// console.log(configer.url)
const instance = axios.create({
    baseURL: config.baseURL,
    headers: config.headers,
    transformResponse: [function(data) {}]
})

instance.interceptors.request.use(
    config => {
        var token = window.sessionStorage.getItem('token')
        if (token) {
            config.headers.taxToken = token
        }
        return config
    },
    error => {
        if (error.code === 5) {
            alert('请重新登录')
            // return service.request(originalRequest)
        }
        const errorInfo = error.response
        console.log(errorInfo)
        return error
    }
)

instance.interceptors.response.use(
    response => {
        let data
        if (response.data === undefined) { // IE9
            data = response.request.responseText
        } else {
            data = response.data
        }
        data = JSON.parse(data)
        console.log(data)
        if (data.code == 5) { // 未登录
            // sessionStorage.clear()
            sessionStorage.removeItem('token');
            this.$message(res.message);
            this.$router.push({ path: '/' })
            console.log(process.env.NODE_ENV)

        }
        return data
    },
    err => {
        if (err && err.response) {
            switch (err.response.status) {
                case 400:
                    err.message = '请求错误'
                    break

                case 401:
                    err.message = '未授权，请登录'
                    break

                case 404:
                    err.message = '404'
                    break

                default:
            }
        }
        return err
    }
)

export default instance